package com.edu.study.base.rest;

import com.edu.study.base.StuAnswerResultDetail.service.IStuAnswerResultDetailService;
import com.edu.study.base.StuAnswerResultErrorDetail.service.IStuAnswerResultErrorDetailService;
import com.edu.study.base.stuAnswerResultSummary.domain.StuAnswerResultSummary;
import com.edu.study.base.stuAnswerResultSummary.service.IStuAnswerResultSummaryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "答题结果查询接口")
@RestController
@RequestMapping("/api/answerResult/")
public class AppAnswerResultController extends BaseController {


    @Autowired
    private IStuAnswerResultSummaryService stuAnswerResultSummaryService;

    @Autowired
    private IStuAnswerResultDetailService stuAnswerResultDetailService;

    @Autowired
    private IStuAnswerResultErrorDetailService stuAnswerResultErrorDetailService;


    @ApiOperation("查询答题结果,包含总分数和每道题的对错结果")
    @GetMapping("getResultByRelationIdAndUserId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "relationId", value = "关联id：type==1,传课程id，type==2,传题卷id",required = true),
            @ApiImplicitParam(name = "userId", value = "用户id",required = true),
            @ApiImplicitParam(name = "type", value = "类型：type==1课程 type==2",required = true),
            @ApiImplicitParam(name = "isHightest", value = "是否查询最高成绩，0否1是",required = true),
    })
    public AjaxResult getResultByRelationId(@RequestParam("relationId") Long relationId,
                                            @RequestParam("userId") Long userId,
                                            @RequestParam("type") Integer type,
                                            @RequestParam("isHightest") Integer isHightest){

        //获得所有题的结果
        StuAnswerResultSummary summary = new StuAnswerResultSummary();
        summary.setRelationId(relationId);
        summary.setAppUserId(userId);
        summary.setType(type);
        summary.setDeleteFlag("0");
        List<StuAnswerResultSummary> summaries = stuAnswerResultSummaryService.selectStuAnswerResultSummaryList(summary);
        //isHightest==1 查询成绩最高的结果
        if(isHightest!=null && isHightest == 1){
            summaries = summaries.stream().sorted((e1, e2) -> Double.compare(e1.getScoreValue().doubleValue(), e2.getScoreValue().doubleValue())).collect(Collectors.toList());
        }else{
            summaries = summaries.stream().sorted((e1, e2) -> Double.compare(e1.getId(), e2.getId())).collect(Collectors.toList());
        }

        if(summaries != null && summaries.size() > 0){
            return AjaxResult.success(summaries.get(0));
        }
        return AjaxResult.error("暂无成绩信息");
    }
}
